package com.hzy.pms.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.hzy.pms.domain.bo.SkuSnapshotBo;
import com.hzy.pms.domain.vo.SkuSnapshotVo;
import com.hzy.pms.domain.SkuSnapshot;
import com.hzy.pms.mapper.SkuSnapshotMapper;
import com.hzy.pms.service.ISkuSnapshotService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * sku信息Service业务层处理
 *
 * @author zengxin
 * @date 2023-10-17
 */
@RequiredArgsConstructor
@Service
public class SkuSnapshotServiceImpl implements ISkuSnapshotService {

    private final SkuSnapshotMapper baseMapper;

    /**
     * 查询sku信息
     */
    @Override
    public SkuSnapshotVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询sku信息列表
     */
    @Override
    public TableDataInfo<SkuSnapshotVo> queryPageList(SkuSnapshotBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<SkuSnapshot> lqw = buildQueryWrapper(bo);
        Page<SkuSnapshotVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询sku信息列表
     */
    @Override
    public List<SkuSnapshotVo> queryList(SkuSnapshotBo bo) {
        LambdaQueryWrapper<SkuSnapshot> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<SkuSnapshot> buildQueryWrapper(SkuSnapshotBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<SkuSnapshot> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getSkuId() != null, SkuSnapshot::getSkuId, bo.getSkuId());
        lqw.eq(bo.getProductId() != null, SkuSnapshot::getProductId, bo.getProductId());
        lqw.eq(StringUtils.isNotBlank(bo.getOutSkuId()), SkuSnapshot::getOutSkuId, bo.getOutSkuId());
        lqw.eq(bo.getPrice() != null, SkuSnapshot::getPrice, bo.getPrice());
        lqw.eq(StringUtils.isNotBlank(bo.getSpData()), SkuSnapshot::getSpData, bo.getSpData());
        return lqw;
    }

    /**
     * 新增sku信息
     */
    @Override
    public Boolean insertByBo(SkuSnapshotBo bo) {
        SkuSnapshot add = BeanUtil.toBean(bo, SkuSnapshot.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改sku信息
     */
    @Override
    public Boolean updateByBo(SkuSnapshotBo bo) {
        SkuSnapshot update = BeanUtil.toBean(bo, SkuSnapshot.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(SkuSnapshot entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除sku信息
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
